Public Class MyRegions

    Public Class Region
        Private m_objSection5 As Section5
        Private m_objSection6 As Section6
        Private m_objSection10 As Section10

        Public Sub New()
            Me.New(New Section5, New Section6, New Section10)
        End Sub

        Public Sub New(ByVal mySection5 As Section5, ByVal mySection6 As Section6, ByVal mySection10 As Section10)
            m_objSection5 = mySection5
            m_objSection6 = mySection6
            m_objSection10 = mySection10
        End Sub

        Public ReadOnly Property Section5() As Section5
            Get
                Return m_objSection5
            End Get
        End Property

        Public ReadOnly Property Section6() As Section6
            Get
                Return m_objSection6
            End Get
        End Property

        Public ReadOnly Property Section10() As Section10
            Get
                Return m_objSection10
            End Get
        End Property

    End Class

    Public Class RegionException
        Inherits Exception

        Public Sub New(ByVal message As String)
            MyBase.New(message)
        End Sub

    End Class

    Private Const MAX_RGS As Integer = 50
    Private Const NRGS_DEFAULT As Integer = 3

    Private m_RegionList As New List(Of Region)
    Private m_UndoRegionList As New List(Of Region)

    Public Event Changed()

    Public Sub New()
        Me.Resize(NRGS_DEFAULT)
    End Sub

    Public Overloads Sub Add()
        Me.AddRegion()
        RaiseEvent Changed()
    End Sub

    Public Overloads Sub Add(ByVal sourceRegion As Integer)
        Me.CheckAdd()
        m_RegionList.Add(Me.CloneRegion(sourceRegion))
        RaiseEvent Changed()
    End Sub

    Public Sub ClearUndo()
        m_UndoRegionList.Clear()
    End Sub

    Public Function Count() As Integer
        Return m_RegionList.Count
    End Function

    Public Overloads Sub Insert(ByVal region As Integer)
        Me.CheckAdd()
        m_RegionList.Insert(region - 1, New Region)
        RaiseEvent Changed()
    End Sub

    Public Overloads Sub Insert(ByVal region As Integer, ByVal sourceRegion As Integer)
        Me.CheckAdd()
        m_RegionList.Insert(region - 1, Me.CloneRegion(sourceRegion))
        RaiseEvent Changed()
    End Sub

    Public ReadOnly Property Item(ByVal region As Integer) As Region
        Get
            Return m_RegionList.Item(region - 1)
        End Get
    End Property

    Public Function ItemList() As List(Of String)
        Dim NewList As New List(Of String)

        For intItem As Integer = 1 To Me.Count
            NewList.Add(Language.ReadMessages("msgREGION") + " " + intItem.ToString)
        Next

        Return NewList
    End Function

    Public Sub RemoveAt(ByVal region As Integer)
        Me.RemoveRegion(region)
        RaiseEvent Changed()
    End Sub

    Public Sub Resize(ByVal count As Integer)
        If count <> Me.Count Then
            If count > Me.Count Then
                For intIndex As Integer = 1 To count - Me.Count
                    Me.AddRegion()
                Next
            Else
                For intIndex As Integer = Me.Count - 1 To count
                    Me.RemoveRegion(intIndex)
                Next
            End If
        End If
    End Sub

    Public Sub SetUndo()
        Me.ClearUndo()
        For intIndex As Integer = 1 To Me.Count
            m_UndoRegionList.Add(Me.CloneRegion(intIndex))
        Next
    End Sub

    Public Sub Undo()
        m_RegionList.Clear()
        For Each objRegion As Region In m_UndoRegionList
            m_RegionList.Add(New Region(CType(objRegion.Section5.Clone, Section5), CType(objRegion.Section6.Clone, Section6), CType(objRegion.Section10.Clone, Section10)))
        Next
        Me.ClearUndo()
        RaiseEvent Changed()
    End Sub


    Private Sub AddRegion()
        Me.CheckAdd()
        m_RegionList.Add(New Region)
    End Sub

    Private Sub CheckAdd()
        If Me.Count = MAX_RGS Then
            Throw New RegionException(String.Format(Language.ReadMessages("msgMAXRGS"), MAX_RGS))
        End If
    End Sub

    Private Sub CheckRemove()
        If Me.Count = 1 Then
            Throw New RegionException(Language.ReadMessages("msgMINRGS"))
        End If
    End Sub

    Private Function CloneRegion(ByVal sourceRegion As Integer) As Region
        Dim objSourceRegion As Region = Me.Item(sourceRegion)

        Return New Region(CType(objSourceRegion.Section5.Clone, Section5), CType(objSourceRegion.Section6.Clone, Section6), CType(objSourceRegion.Section10.Clone, Section10))
    End Function

    Private Sub RemoveRegion(ByVal region As Integer)
        Me.CheckRemove()
        m_RegionList.RemoveAt(region - 1)
    End Sub

End Class
